<template>
  <el-dialog
    :title="'分配详情-' + questionCon.questionNumbers"
    width="500px"
    :visible.sync="allocateschoolishow"
    :before-close="handleClose"
  >
    <p>
      剩余任务量：{{
        questionCon.remainingCount ? questionCon.remainingCount : 0
      }}
    </p>
    <el-table
      ref="multipleTable"
      :data="taskschoollist"
      tooltip-effect="dark"
      style="width: 100%"
      border
      stripe
      :header-cell-style="{
        background: '#D8E4F0',
        color: '#2A303B',
        fontWeight: '500',
      }"
      height="295"
    >
      <el-table-column align="center" label="分配学校">
        <template slot-scope="scope">
          {{ scope.row.schoolName || "-" }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="任务总量">
        <template slot-scope="scope">
          <span v-if="scope.row.isDisabled">{{ scope.row.taskCount }}</span>
          <el-input
            v-else
            v-model="scope.row.disassemblytask"
            @change="inputchangebox(scope.$index)"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column align="center" label="已评">
        <template slot-scope="scope">
          {{ scope.row.reviewedCount || "-" }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="剩余">
        <template slot-scope="scope">
          {{ scope.row.remainingCount || "-" }}
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" fixed="right" label="操作" align="center" width="180">
            <template slot-scope="scope">
                <div style="display: flex;text-decoration: underline;" class="scopednr">
                    <b v-if="questionCon.remainingCount==null||questionCon.remainingCount==0" style="color:#999;">添加任务</b>
                    <b v-else @click="addtaskrw(scope.row,scope.$index)">添加任务</b>
                    {{ taskschoollist.len }}
                    <b style="color:#999;" v-if="taskschoollist.length==1">拆解任务</b>
                    <b @click="disassemblytask(scope.row,scope.$index)" v-else>拆解任务</b>
                </div>
            </template>
            </el-table-column> -->
    </el-table>
    <div class="objbtnbox">
      <span
        v-if="questionCon.taskStatus == 1"
        style="background: #eee; color: #666"
        >启用中</span
      >
      <span @click="qytapproject" v-else>启用</span>
      <!-- <span @click="alltaskschoolSure">保存</span> -->
      <span @click="alltaskschoolBack">返回</span>
    </div>
  </el-dialog>
</template>

<script>
import {
  singQuestionassignment,
  taskschoolUpdateSchoolTaskCount,
  taskschoolDetail,
} from "@/api/task/task.js";
export default {
  props: {
    allocateschoolishow: false,
    questionCon: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      cjindex: 1,
      taskschoollist: [],
    };
  },
  created() {
    this.taskschooltap();
  },
  methods: {
    handleClose() {
      this.$emit("alltaskschoolSureemit", 2);
    },
    taskschooltap() {
      let obj = {
        assignmentConfigId: this.questionCon.id,
      };
      taskschoolDetail(obj).then((res) => {
        if (res.code == 200) {
          this.taskschoollist = res.data;
          this.taskschoollist = res.data.map((item) => {
            return {
              ...item,
              isDisabled: true,
              disassemblytask: item.taskCount,
            };
          });
        }
      });
    },
    alltaskschoolSure() {
      let obj = {
        assignmentConfigId: this.questionCon.id,
        totalRemainingCount: this.questionCon.remainingCount,
        schoolTasks: this.taskschoollist.map((item) => {
          return {
            schoolId: item.schoolId,
            taskCount: item.taskCount,
            remainingCount: item.remainingCount,
          };
        }),
      };
      taskschoolUpdateSchoolTaskCount(obj).then((res) => {
        if (res.code == 200) {
          this.$message.success("分配成功");
          this.$emit("alltaskschoolSureemit", 1);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    alltaskschoolBack() {
      this.$emit("alltaskschoolSureemit", 2);
    },
    qytapproject() {
      let obj = {
        id: this.questionCon.id,
        taskStatus: 1,
      };
      singQuestionassignment(obj).then((res) => {
        if (res.code == 200) {
          this.$emit("rowreviewrounds", id);
          this.$message({
            message: "启用成功",
            type: "success",
          });
        }
      });
    },
    //拆解任务
    disassemblytask(con, index) {
      this.taskschoollist.forEach((item) => {
        item.isDisabled = true;
      });
      this.taskschoollist[index].isDisabled = false;
      this.cjindex = 1;
    },
    //添加任务
    addtaskrw(con, index) {
      this.taskschoollist.forEach((item) => {
        item.isDisabled = true;
      });
      this.taskschoollist[index].isDisabled = false;
      this.cjindex = 2;
    },
    inputchangebox(index) {
      if (this.cjindex == 1) {
        this.questionCon.remainingCount =
          this.taskschoollist[index].taskCount -
          this.taskschoollist[index].disassemblytask;
        this.taskschoollist[index].remainingCount =
          this.taskschoollist[index].remainingCount -
          parseInt(
            this.taskschoollist[index].taskCount -
              this.taskschoollist[index].disassemblytask
          );
        this.taskschoollist[index].taskCount =
          this.taskschoollist[index].disassemblytask;
      } else {
        console.info(
          "this.taskschoollist[index].taskCount",
          parseInt(
            this.taskschoollist[index].disassemblytask -
              this.taskschoollist[index].taskCount
          )
        );
        console.info(
          "this.questionCon.remainingCount",
          this.questionCon.remainingCount
        );
        if (
          this.questionCon.remainingCount <
          parseInt(
            this.taskschoollist[index].disassemblytask -
              this.taskschoollist[index].taskCount
          )
        ) {
          this.$message({
            message: "添加任务的数量不能超过剩余任务量",
            type: "warning",
          });
        } else {
          this.questionCon.remainingCount =
            this.questionCon.remainingCount -
            parseInt(
              this.taskschoollist[index].disassemblytask -
                this.taskschoollist[index].taskCount
            );
          this.taskschoollist[index].remainingCount =
            this.taskschoollist[index].remainingCount +
            parseInt(
              this.taskschoollist[index].disassemblytask -
                this.taskschoollist[index].taskCount
            );
          this.taskschoollist[index].taskCount =
            this.taskschoollist[index].disassemblytask;
        }
      }

      this.taskschoollist[index].isDisabled = true;
    },
  },
};
</script>

<style scoped>
.objsubMain {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.75);
  z-index: 100;
}
.objsubContent {
  box-sizing: border-box;
  width: 1040px;
  height: 500px;
  background: #ffffff;
  box-shadow: 0px 4px 16px 0px rgba(85, 114, 146, 0.1);
  border-radius: 10px;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 24px;
}
.objsubContent h3 {
  font-size: 18px;
  font-weight: normal;
}
.objsubattention {
  display: flex;
  font-size: 14px;
  color: #619cf5;
  padding: 10px 0 24px;
}
.custom-table {
  width: 100%;
  border: 1px solid rgba(216, 228, 240, 0.34);
  border-right: none;
  border-bottom: none;
}
/* 自定义表格内部元素的样式 */
.custom-table >>> .el-table__header-wrapper th {
  background-color: #e8f0fa;
  height: 42px;
  padding: 0;
  text-align: center;
  font-weight: normal;
}
.custom-table >>> .el-table th.el-table__cell.is-leaf,
.custom-table >>> .el-table td.el-table__cell {
  border-bottom: none;
}
.custom-table >>> .el-table__row td {
  height: 42px;
  padding: 0;
  text-align: center;
  border-right: solid 1px rgba(216, 228, 240, 0.5);
  font-size: 14px;
}
.el-table .warning-row {
  background: #ffffff;
}
.el-table .success-row {
  background: #d8e4f0;
}
.el-table .el-table__cell {
  padding: 8px 0;
}
.objbtnbox {
  display: flex;
  align-items: center;
  padding-top: 15px;
}
.objbtnbox span {
  width: 84px;
  height: 34px;
  background: #1a48db;
  border-radius: 4px;
  text-align: center;
  line-height: 34px;
  color: #fff;
  display: inline-block;
  margin-right: 15px;
  cursor: pointer;
}
.objbtnbox span:last-child {
  background: #ec555a;
}
.scopednr b {
  font-weight: normal;
  display: inline-block;
  padding-right: 20px;
  cursor: pointer;
  color: #1a48db;
}
</style>